﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections.Generic;

/// <summary>
/// Descripción breve de PlayerHighScore
/// </summary>
public class PlayerHighScore
{
    public string Nombre;
    public int Maquina;
    public double Puntuacion;
    public bool WPPR;
    public string Email;
    public bool EnvioEmail;


    public PlayerHighScore(string p_Nombre,int p_Maquina,double p_Puntuacion,bool p_WPPR,string p_Email,bool p_EnvioEmail)
    {
        this.Email = p_Email;
        this.Nombre = p_Nombre;
        this.Puntuacion = p_Puntuacion;
        this.WPPR = p_WPPR;
        this.EnvioEmail = p_EnvioEmail;
        this.Maquina = p_Maquina;
    }

    public PlayerHighScore(string p_Nombre, int p_Maquina, double p_Puntuacion)
    {
        this.Nombre = p_Nombre;
        this.Puntuacion = p_Puntuacion;
        this.Maquina = p_Maquina;
    }


    public static bool CreatePlayer(PlayerHighScore p_Player)
    {
        OleDbConnection objConnection = new OleDbConnection();
        try
        {
            //TODO controlar si hay un playerNumber duplicado

            objConnection = DataBase.GetConnection();
            OleDbCommand objCommand = new OleDbCommand("INSERT INTO HighScore (Nombre,Maquina,Puntuacion,WPPR,Email,EnvioEmail) VALUES ('" +
                                                        p_Player.Nombre.ToUpper() + "'," + p_Player.Maquina + "," + p_Player.Puntuacion
                                                        + "," + p_Player.WPPR + ",'" + p_Player.Email.ToString() + "'," + p_Player.EnvioEmail + ")", objConnection);
            objConnection.Open();
            objCommand.ExecuteNonQuery();

            OleDbCommand cmdGetIdentity = new OleDbCommand();
            cmdGetIdentity.CommandText = "SELECT @@IDENTITY";
            cmdGetIdentity.Connection = objConnection;

            return true;

        }
        catch (Exception Ex)
        {
            return false;
        }
        finally
        {
            objConnection.Close();
        }
    }

    public static int GetTotalHighScoreMachines() {
        try
        {         

            
            Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

            DataTable oDataTable = DataBase.dt("SELECT * FROM HighScoreMachines");

            return oDataTable.Rows.Count; 
           
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }
    public static List<PlayerHighScore> GetPlayersHighScore(int Machine,out string MachineName)
    {
        try
        {
            string Name = string.Empty;

            List<HighScoreMachines> omachine = HighScoreMachines.LoadHighScoreMachines();

            foreach (HighScoreMachines iHighScoreMachines in omachine) {
                if (iHighScoreMachines.Machine == Machine)
                {
                    Name = iHighScoreMachines.MachineName;
                }
            }

            MachineName = Name;
            Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
           
            DataTable oDataTable = DataBase.dt("SELECT TOP " + Convert.ToInt32(rootWebConfig.AppSettings.Settings["HighScorePlayersList"].Value) + "  Nombre,Maquina,Puntuacion FROM HighScore WHERE Maquina=" + Machine.ToString() + " ORDER BY Puntuacion DESC");

            List<PlayerHighScore> ReturnList = new List<PlayerHighScore>();

            foreach (DataRow iRow in oDataTable.Rows)
            {
                PlayerHighScore oPlayerHighScore = new PlayerHighScore(iRow["Nombre"].ToString(),int.Parse(iRow["Maquina"].ToString()),long.Parse(iRow["Puntuacion"].ToString()));
                ReturnList.Add(oPlayerHighScore);
            }
            return ReturnList;
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }


}
